package modelUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ResourceBundle;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import usuario.usuarioTO.UsuarioTO;

public class Configuracion {

	private String MultimediaDAOclass;
	private String PlantillaDAOclass;
	private String UsuarioDAOclass;
	private String DirUsuarios;
	private String DirUsuarioActual;
	private String TecladoActual;
	private String ListaSonidos;
	private String ListaImagenes;
	private String Idioma;
	private String Barrido;
	private String TiempoBarrido;
	private String Creditos;
	private static Configuracion instancia;
	private String Test;
	private UsuarioTO user;
	private String posicion;
	private String anchoTeclado;
	private String altoTeclado;
	
	//Constructor privado para el singleton
	private Configuracion(){
		leerDoc();
	}
	
	public static Configuracion instancia(){
		if (instancia==null) instancia = new Configuracion();
		return instancia;
	}

	public void leerDoc() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document documento = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			documento = builder.parse(new File("Config.xml"));

			Node nodoRaiz = documento.getFirstChild();

			// Obtener los atributos de un nodo
			NodeList atributos = nodoRaiz.getChildNodes();
			for (int i = 0; i < atributos.getLength(); i++) {
				Node nodo = atributos.item(i);
				if (nodo instanceof Element) {
					if (nodo.getNodeName().equals("MultimediaDAOclass")) {
						MultimediaDAOclass = nodo.getTextContent();

					}
					if (nodo.getNodeName().equals("UsuarioDAOclass")) {
						UsuarioDAOclass = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("PlantillaDAOclass")) {
						PlantillaDAOclass = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("DirUsuarios")) {
						DirUsuarios = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("DirUsuarioActual")) {
						DirUsuarioActual = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("TecladoActual")) {
						TecladoActual = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("ListaSonidos")) {
						ListaSonidos = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("ListaImagenes")) {
						ListaImagenes = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("Idioma")) {
						Idioma = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("Barrido")) {
						Barrido = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("TiempoBarrido")) {
						TiempoBarrido = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("Creditos")) {
						Creditos = nodo.getTextContent();
					}
					if (nodo.getNodeName().equals("Test")) {
						Test = nodo.getTextContent();
					}
				}
			}
		} catch (Exception spe) {
			Util.popup(0, ResourceBundle.getBundle("Internacionalizacion/Etiquetas").getString("etiqueta_error_standard_XML"));
		}
	}

	public String getListaImagenes() {
		return ListaImagenes;
	}

	public void setListaImagenes(String listaImagenes) {
		ListaImagenes = listaImagenes;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("ListaImagenes")) {
					raiz.removeChild(nodo);
					Element elemento = doc.createElement("Listaimagenes");
					elemento.setTextContent(ListaImagenes);
					raiz.appendChild(elemento);
				}
			}
		}
		
		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {}
	}

	public String getListaSonidos() {
		return ListaSonidos;
	}

	public void setListaSonidos(String listaSonidos) {
		ListaSonidos = listaSonidos;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");
		} catch (Exception spe) {}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {
				if (nodo.getNodeName().equals("ListaSonidos")) {
					raiz.removeChild(nodo);
					Element elemento = doc.createElement("ListaSonidos");
					elemento.setTextContent(ListaSonidos);
					raiz.appendChild(elemento);

				}
			}
		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {}
	}

	public String getTecladoActual() {
		return TecladoActual;
	}

	public void setTecladoActual(String tecladoActual) {
		TecladoActual = tecladoActual;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("TecladoActual")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("TecladoActual");
					elemento.setTextContent(TecladoActual);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}

	}

	public String getDirUsuarioActual() {
		return DirUsuarioActual;
	}

	public void setDirUsuarioActual(String dirUsuarioActual) {

		DirUsuarioActual = dirUsuarioActual;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("DirUsuarioActual")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("DirUsuarioActual");
					elemento.setTextContent(DirUsuarioActual);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}

	}

	public String getDirUsuarios() {
		return DirUsuarios;
	}

	public void setDirUsuarios(String dirUsuarios) {
		DirUsuarios = dirUsuarios;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("DirUsuarios")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("DirUsuarios");
					elemento.setTextContent(DirUsuarios);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}

	}

	public String getMultimediaDAOclass() {
		return MultimediaDAOclass;
	}

	public void setMultimediaDAOclass(String multimediaDAOclass) {
		MultimediaDAOclass = multimediaDAOclass;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("SonidoDAOclass")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("SonidoDAOclass");
					elemento.setTextContent(MultimediaDAOclass);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}

	}

	

	public String getPlantillaDAOclass() {
		return PlantillaDAOclass;
	}

	public void setPlantillaDAOclass(String plantillaDAOclass) {
		PlantillaDAOclass = plantillaDAOclass;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("PlantillaDAOclass")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("PlantillaDAOclass");
					elemento.setTextContent(PlantillaDAOclass);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}

	}

	public String getUsuarioDAOclass() {
		return UsuarioDAOclass;
	}

	public void setUsuarioDAOclass(String usuarioDAOclass) {
		UsuarioDAOclass = usuarioDAOclass;

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("UsuarioDAOclass")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("UsuarioDAOclass");
					elemento.setTextContent(UsuarioDAOclass);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}

	}
	
	

	public String getIdioma() {
		return Idioma;
	}

	public void setIdioma(String idioma) {
		Idioma = idioma;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("Idioma")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("Idioma");
					elemento.setTextContent(Idioma);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}
		
		
		
		
	}

	
	
	
	public String getBarrido() {
		return Barrido;
	}

	public void setBarrido(String barrido) {
		Barrido = barrido;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("Barrido")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("Barrido");
					elemento.setTextContent(Barrido);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}

	}

	public String getTiempoBarrido() {
		return TiempoBarrido;
	}

	public void setTiempoBarrido(String tiempoBarrido) {
		TiempoBarrido = tiempoBarrido;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("TiempoBarrido")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("TiempoBarrido");
					elemento.setTextContent(TiempoBarrido);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}

	}

	
	
	public String getCreditos() {
		return Creditos;
	}

	public void setCreditos(String creditos) {
		Creditos = creditos;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("Creditos")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("Creditos");
					elemento.setTextContent(Creditos);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}
		
	}

	
	
	public String getTest() {
		return Test;
	}

	public void setTest(String test) {
		Test = test;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc = null;

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse("Config.xml");

		} catch (Exception spe) {

		}

		Node raiz = doc.getFirstChild();
		NodeList lista = raiz.getChildNodes();

		for (int i = 0; i < lista.getLength(); i++) {
			Node nodo = lista.item(i);
			if (nodo instanceof Element) {

				if (nodo.getNodeName().equals("Test")) {

					raiz.removeChild(nodo);
					Element elemento = doc.createElement("Test");
					elemento.setTextContent(Test);
					raiz.appendChild(elemento);

				}

			}

		}

		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			Transformer transformer = transFactory.newTransformer();

			DOMSource source = new DOMSource(doc);
			File newXML = new File("Config.xml");
			FileOutputStream os = new FileOutputStream(newXML);
			StreamResult result = new StreamResult(os);
			transformer.transform(source, result);
		} catch (Exception e) {

		}
		
	}

	public static void main(String[] args) {

		Configuracion conf = new Configuracion();
		conf.leerDoc();



	}

	public UsuarioTO getUser() {
		return user;
	}

	public void setUser(UsuarioTO user) {
		this.user = user;
	}

	public String getPosicion() {
		return posicion;
	}

	public void setPosicion(String posicion) {
		this.posicion = posicion;
	}

	public String getAnchoTeclado() {
		return anchoTeclado;
	}

	public void setAnchoTeclado(String anchoTeclado) {
		this.anchoTeclado = anchoTeclado;
	}

	public String getAltoTeclado() {
		return altoTeclado;
	}

	public void setAltoTeclado(String altoTeclado) {
		this.altoTeclado = altoTeclado;
	}
	
	
	

}
